<template>
  <div>
    <el-card>
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/userpi' }">个人主页</el-breadcrumb-item>
          <el-breadcrumb-item>修改信息</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="modify_main">
        <el-form label-position="left" :model="userinfoForm" ref="userinfoRef" label-width="80px">
          <el-form-item label="UID：">
            <el-input v-model="userinfoForm._id" disabled></el-input>
          </el-form-item>
          <el-form-item label="E-Mail：">
            <el-input v-model="userinfoForm.email" disabled></el-input>
          </el-form-item>
          <el-form-item label="昵称：">
            <el-input v-model="userinfoForm.nickName"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-if="this.userinfo.role == 'admin'" v-model="userinfoForm.status" placeholder="请选择用户状态">
              <el-option label="激活" value="1"></el-option>
              <el-option label="未激活" value="0"></el-option>
            </el-select>
            <el-select v-else v-model="userinfoForm.status" placeholder="请选择用户状态" disabled>
              <el-option label="激活" value="1"></el-option>
              <el-option label="未激活" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="权限：">
            <el-select v-if="this.userinfo.role == 'admin'" v-model="userinfoForm.role" placeholder="请选择活动区域">
              <el-option label="超级管理员" value="admin"></el-option>
              <el-option label="普通用户" value="normal"></el-option>
            </el-select>
            <el-select v-else v-model="userinfoForm.role" placeholder="请选择活动区域" disabled>
              <el-option label="超级管理员" value="admin"></el-option>
              <el-option label="普通用户" value="normal"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('userinfoRef')">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userinfo: {},
      userinfoForm: {}
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取用户信息
    getUserList() {
      this.userinfo = JSON.parse(sessionStorage.getItem('myuser'))
      this.userinfoForm = JSON.parse(sessionStorage.getItem('myuser'))
      this.userinfoForm.status = this.userinfoForm.status + ''
      // console.log(this.userinfoForm)
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return
        const that = this
        const uid = this.userinfoForm._id + ''
        const formdata = this.$qs.stringify(this.userinfoForm)
        await this.$http({
          method: 'put',
          url: '/users/' + uid,
          data: formdata
        }).then((res) => {
          window.sessionStorage.setItem('myuser', JSON.stringify(res.data))
          // console.log(res)
          that.$message({
            showClose: true,
            message: '信息修改成功！',
            type: 'success'
          })
        }).catch((err) => {
          const messg = err.response.data
          that.$message({
            showClose: true,
            message: messg,
            type: 'error'
          })
        })
      })
    }
  }
}
</script>

<style scoped>
.modify_main {
  margin: 2px auto;
  width: 520px;
}
</style>
